package com.lanou;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by lanou on 18/1/23.
 */
public class Interceptor implements HandlerInterceptor {



    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        // 1、先去session当中获取当前登录用户
        // 2、检查用户是否存在，如果存在则证明用户登录，否则跳转到登录页面
        String username = (String)httpServletRequest.getSession().getAttribute("uname");
		/*if(username != null && !"".equals(username)) {

		}*/
        if(!StringUtils.isEmpty(username)) {
            return true;
        } else {
            // 如果没有登录，则返回登录页面
            httpServletResponse.sendRedirect("/login");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
